﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Configuration;
using System.Configuration.Install;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.ServiceModel;
using System.ServiceModel.Discovery;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Text;
using System.Threading;

using Microsoft.Win32;
using Wcf_Print;
using LogClass;


namespace Test_Print_Service
{

    class Program
    {
        public static ServiceHost serviceHost = null;
        private static bool blog;
        private const string COMPANY = "PGMariotti.net";
        private const string PRODUCT_NAME = "WP7 Print Helper Service";
        private const string MODULE_NAME = "Configuration";
        private static ServiceBase[] ServicesToRun;
        private static RegistryKey reg_key;
        private static string sPort = "8000";
        private static string sSearch = "http://localhost:";

        static void Main(string[] args)
        {
            OnStart(new string[1] { "OK" });
            if ((ConfigurationManager.AppSettings["LoggingEnabled"] == "Yes") ||
                (ConfigurationManager.AppSettings["LoggingEnabled"] == "Full"))
                blog = true;
            else
                blog = false;

            LogIt.LogInit(ConfigurationManager.AppSettings["LogName"], "Test Print Service", blog);
            if (ConfigurationManager.AppSettings["LoggingEnabled"] == "Full")
                LogIt.bFullLog = true;
            else
                LogIt.bFullLog = false;

            string str = "";
            PrintWcfService pwcf = new PrintWcfService();
            pwcf.GetDefaultPrinter(out str);
            pwcf.GetPrinters(out str);
        }

        protected static void OnStart(string[] args)
        {
            string reason = "";
            if (serviceHost != null)
            {
                serviceHost.Close();
            }

            RegistryKey reg_key = Registry.LocalMachine.OpenSubKey("Software", true);
            RegistryKey reg_key1 = reg_key;
            String szKey = COMPANY + "\\" + PRODUCT_NAME + "\\" + MODULE_NAME;
            reg_key = reg_key1.OpenSubKey(szKey, true);
            if (reg_key != null)
                sPort = reg_key.GetValue("Port", "8000").ToString();
            else
            {
                reason = "Registry key not found";
                sPort = "8000";
            }

            string sExec = Assembly.GetExecutingAssembly().Location;
            int i = sExec.LastIndexOf("\\");
            string sDir = sExec.Substring(0, i) + "\\WP7PrintService.exe.config";
            StreamReader sr = new StreamReader(sDir);
            string sConfig = sr.ReadToEnd();
            sr.Close();
            i = sConfig.IndexOf(sSearch);
            int j = sConfig.IndexOf(":", i + 6);
            int k = sConfig.IndexOf("/", j);
            string str = sConfig.Substring(0, j + 1) + sPort.ToString() + sConfig.Substring(k);
            sConfig = str;
            i = sConfig.LastIndexOf(sSearch);
            j = sConfig.IndexOf(":", i + 6);
            k = sConfig.IndexOf("/", j);
            str = sConfig.Substring(0, j + 1) + sPort.ToString() + sConfig.Substring(k);
            sConfig = str;
            StreamWriter sw = new StreamWriter(sDir, false);
            sw.Write(sConfig);
            sw.Close();

            ServiceHost host = new ServiceHost(typeof(PrintWcfService));
            host.Faulted += new EventHandler(HostHasFaulted);
            host.Description.Behaviors.Add(new ErrorServiceBehavior());

            //ServiceHost host = new ServiceHost(typeof(PrintWcfService));
            //host.Faulted += new EventHandler(HostHasFaulted);
            //host.Description.Behaviors.Add(new ErrorServiceBehavior());

            //host.Open();
            //LogIt.LogInit(ConfigurationManager.AppSettings["LogName"], ServiceName, blog);
            //string sLog = this.ServiceName + " Started on port " + sPort.ToString();
            //if (reason != "")
            //    sLog += (" (default port because " + reason);
            //LogIt.ForceLog(sLog);
        }

    }
}
